//
//  MapTransitRouteListController.m
//  TMapDemo
//
//  Created by zhaoxibo on 13-5-28.
//
//

#import "MapTransitRouteListController.h"

@interface MapTransitRouteListController ()

@end

@implementation MapTransitRouteListController

@synthesize _tableView;
@synthesize pTransitRouteResult = _pTransitRouteResult;
@synthesize _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self set_tableView:nil];
    [super viewDidUnload];
}

- (NSString *)getWalkDescriptionWithIndex:(NSInteger)iIndex routeLine:(TTransitRouteLine *)pLine {
    NSString *strDescript = nil;
    NSInteger iSegmentCount = [pLine.segment count];
    TTransitRouteSegment *pSegment = [pLine.segment objectAtIndex:iIndex];
    if (pSegment == nil ) {
        return nil;
    }
    TTransitRouteSegmentLine *pSegmentLine = [pSegment.segmentLine objectAtIndex:0];
    NSInteger iDistance = pSegmentLine.distance;
    NSString *strStart = pSegment.start.name;
    NSString *strEnd = pSegment.end.name;
    
    if (iIndex == 0) {
        // 第一项
        if ([strStart length] == 0)
            strDescript = [[NSString alloc] initWithFormat:@"步行%d米,到达%@", iDistance, strEnd];
        else
            strDescript = [[NSString alloc] initWithFormat:@"从%@步行%d米,到达%@", strStart, iDistance, strEnd];
    }
    else if (iIndex > 0 && iIndex < iSegmentCount - 1) {
        // 若不是第一项，取上一项的终点
        TTransitRouteSegment *pSegmentPrev = [pLine.segment objectAtIndex:iIndex - 1];
        if (pSegmentPrev) {
            NSString *strEndPrev = pSegmentPrev.end.name;
            if ([strStart compare:strEndPrev] == 0)   // 起点与上一条终点相同
                strDescript = [[NSString alloc] initWithFormat:@"步行%d米,到达%@", iDistance, strEnd];
            else
                strDescript = [[NSString alloc] initWithFormat:@"从%@步行%d米,到达%@", strStart, iDistance, strEnd];
        }
    }
    else if (iIndex == iSegmentCount - 1) {
        // 最后一项
        TTransitRouteSegment *pSegmentPrev = [pLine.segment objectAtIndex:iIndex - 1];
        if (pSegmentPrev) {
            NSString *strEndPrev = pSegmentPrev.end.name;
            if ([strStart compare:strEndPrev] == 0) {
                if ([strEnd length] == 0)
                    strDescript = [[NSString alloc] initWithFormat:@"步行%d米,到达终点站", iDistance];
                else
                    strDescript = [[NSString alloc] initWithFormat:@"步行%d米,到达%@", iDistance, strEnd];
            }
            else {
                if ([strEnd length] == 0)
                    strDescript = [[NSString alloc] initWithFormat:@"从%@步行%d米,到达终点站", strStart, iDistance];
                else
                    strDescript = [[NSString alloc] initWithFormat:@"从%@步行%d米,到达%@", strStart, iDistance, strEnd];
            }
        }
    }
    
    [strDescript autorelease];
    return strDescript;
}

- (NSString *)getBusDescriptionWithIndex:(NSInteger)iIndex routeLine:(TTransitRouteLine *)pLine {
    NSString *strDescript = nil;
    NSInteger iSegmentCount = [pLine.segment count];
    TTransitRouteSegment *pSegment = [pLine.segment objectAtIndex:iIndex];
    if (pSegment == nil ) {
        return nil;
    }
    TTransitRouteSegmentLine *pSegmentLine = [pSegment.segmentLine objectAtIndex:0];
    NSString *strStart = pSegment.start.name;
    NSString *strEnd = strEnd = pSegment.end.name;
    NSString *strSegmentLineName = pSegmentLine.name;  // 公交路名
    NSInteger iStationCount = pSegmentLine.stationCount; // 站数
    
    if (iIndex == 0) {
        // 第一项
        if ([strStart length] == 0)
            strDescript = [[NSString alloc] initWithFormat:@"乘坐%@,经过%d站,到达%@", strSegmentLineName, iStationCount, strEnd];
        else {
            strDescript = [[NSString alloc] initWithFormat:@"从%@乘坐%@,经过%d站,到达%@", strStart, strSegmentLineName, iStationCount, strEnd];
        }
    }
    else if (iIndex > 0 && iIndex < iSegmentCount - 1) {
        // 若不是第一项，取上一项的终点
        TTransitRouteSegment *pSegmentPrev = [pLine.segment objectAtIndex:iIndex - 1];
        if (pSegmentPrev) {
            NSString *strEndPrev = pSegmentPrev.end.name;
            if ([strStart compare:strEndPrev] == 0)   // 起点与上一条终点相同
                strDescript = [[NSString alloc] initWithFormat:@"乘坐%@,经过%d站,到达%@", strSegmentLineName, iStationCount, strEnd];
            else
                strDescript = [[NSString alloc] initWithFormat:@"从%@乘坐%@,经过%d站,到达%@", strStart, strSegmentLineName, iStationCount, strEnd];
        }
    }
    else if (iIndex == iSegmentCount - 1) {
        // 最后一项
        TTransitRouteSegment *pSegmentPrev = [pLine.segment objectAtIndex:iIndex - 1];
        if (pSegmentPrev) {
            NSString *strEndPrev = pSegmentPrev.end.name;
            if ([strStart compare:strEndPrev] == 0) {
                if ([strEnd length] == 0)
                    strDescript = [[NSString alloc] initWithFormat:@"乘坐%@,经过%d站,到达终点站", strSegmentLineName, iStationCount];
                else
                    strDescript = [[NSString alloc] initWithFormat:@"乘坐%@,经过%d站,到达%@", strSegmentLineName, iStationCount, strEnd];
            }
            else {
                if ([strEnd length] == 0)
                    strDescript = [[NSString alloc] initWithFormat:@"从%@乘坐%@,经过%d站,到达终点站", strStart, strSegmentLineName, iStationCount];
                else
                    strDescript = [[NSString alloc] initWithFormat:@"从%@乘坐%@,经过%d站,到达%@", strStart, strSegmentLineName, iStationCount, strEnd];
            }
        }
    }
    [strDescript autorelease];
    return strDescript;
}

- (NSString *)getSubwayTransferDescriptionWithIndex:(NSInteger)iIndex routeLine:(TTransitRouteLine *)pLine {
    NSString *strDescript = nil;
    TTransitRouteSegment *pSegment = [pLine.segment objectAtIndex:iIndex];
    if (pSegment) {
        TTransitRouteSegmentLine *pSegmentLine = [pSegment.segmentLine objectAtIndex:0];
        strDescript = [[NSString alloc] initWithFormat:@"站内换乘%@", pSegmentLine.name];// 公交路名
        [strDescript autorelease];
        return strDescript;
    }
    return nil;
}

- (NSString *)getOtherSegmentLineName:(TTransitRouteSegment *)pSegment {
    NSString *strOtherLineName = nil;
    NSString *strTemp = nil;
    TTransitRouteSegmentLine *pSegmentLine = nil;
    int i = 1;
    if ([pSegment.segmentLine count] > 1) {
        strOtherLineName = @"还可乘坐:";
        for (; i < [pSegment.segmentLine count]; i++) {
            if (i > 1)
                strOtherLineName = [strOtherLineName stringByAppendingString:@","];
            pSegmentLine = [pSegment.segmentLine objectAtIndex:i];
            strTemp = pSegmentLine.name;
            strOtherLineName = [strOtherLineName stringByAppendingString:strTemp];
        }
        return strOtherLineName;
    }
    return nil;
}

#pragma mark -
#pragma mark UIViewController delegate methods
// called after this controller's view will appear
- (void)viewWillAppear:(BOOL)animated {
    // 设置标题
    self.title = @"公交规划结果";
}

#pragma mark -
#pragma mark ---------UITableViewDataSource---------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_pTransitRouteResult == nil ) {
        return 0;
    }
    TTransitRouteSingleTypeResult *pSingleResult = [_pTransitRouteResult.result objectAtIndex:0];
    return [pSingleResult.line count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_pTransitRouteResult == nil ) {
        return 0;
    }
    TTransitRouteSingleTypeResult *pSingleResult = [_pTransitRouteResult.result objectAtIndex:0];
    if (section < 0 || section > [pSingleResult.line count]) {
        return 0;
    }
    TTransitRouteLine *pLine = [pSingleResult.line objectAtIndex:section];
    return [pLine.segment count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_pTransitRouteResult == nil ) {
        return nil;
    }
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    NSInteger iSection = [indexPath section];
    NSInteger iRow = [indexPath row];
    TTransitRouteSingleTypeResult *pSingleResult = [_pTransitRouteResult.result objectAtIndex:0];
    if (iSection < 0 || iSection > [pSingleResult.line count]) {
        return nil;
    }
    TTransitRouteLine *pLine = [pSingleResult.line objectAtIndex:iSection];
    if (iRow < 0 || iRow > [pLine.segment count]) {
        return nil;
    }
    NSString *strText = nil;
    NSString *strDetailText = nil;
    TTransitRouteSegment *pSegment = [pLine.segment objectAtIndex:iRow];
    if (pSegment) {
        switch (pSegment.type) {
            case TRANSIT_SEGMENT_TYPE_WALK:
                strText = [self getWalkDescriptionWithIndex:iRow routeLine:pLine];
                break;
            case TRANSIT_SEGMENT_TYPE_BUS:
            case TRANSIT_SEGMENT_TYPE_SUBWAY:
                strText = [self getBusDescriptionWithIndex:iRow routeLine:pLine];
                break;
            case TRANSIT_SEGMENT_TYPE_SUBWAYTRANSFER:
                strText = [self getSubwayTransferDescriptionWithIndex:iRow routeLine:pLine];
                break;
            default:
                break;
        }
    }
    strDetailText = [self getOtherSegmentLineName:pSegment];
    cell.textLabel.text = [NSString stringWithFormat:@"%d.%@", iRow + 1, strText];
    cell.detailTextLabel.text = strDetailText;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
    cell.textLabel.numberOfLines = 1;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark -
#pragma mark ---------UITableViewDelegate---------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_pTransitRouteResult == nil ) {
        return;
    }
    TTransitRouteSingleTypeResult *pSingleResult = [_pTransitRouteResult.result objectAtIndex:0];
    TTransitRouteLine *pLine = [pSingleResult.line objectAtIndex:indexPath.section];
    if ([_delegate respondsToSelector:@selector(MapTransitRouteListController:selectLine:AtIndex:)]) {
        [_delegate MapTransitRouteListController:self selectLine:pLine AtIndex:indexPath.row];
    }
    [[self navigationController] popViewControllerAnimated:NO];
}

// return 'depth' of row for hierarchies
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; {
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TTransitRouteSingleTypeResult *pSingleResult = [_pTransitRouteResult.result objectAtIndex:0];
    TTransitRouteLine *pLine = nil;
    if (section < 0 || section > [pSingleResult.line count]) {
        return nil;
    }
    UIView *viewSection = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UILabel *textSection = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
    pLine = [pSingleResult.line objectAtIndex:section];
    textSection.text = pLine.name;
    textSection.textColor = [UIColor whiteColor];
    textSection.backgroundColor = [UIColor lightGrayColor];
    textSection.font = [UIFont fontWithName:@"Helvetica" size:16];
    textSection.numberOfLines = 2;
    [viewSection addSubview:textSection];
    viewSection.backgroundColor = [UIColor lightGrayColor];
    [textSection release];
    return [viewSection autorelease];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}


@end
